import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/AppNavigator.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/common/App.dart';
import 'package:huajianshe_app/common/AppRouter.dart';
import 'package:huajianshe_app/common/C.dart';
import 'package:huajianshe_app/common/TS.dart';
import 'package:huajianshe_app/common/space.dart';
import 'package:huajianshe_app/network/Api.dart';
import 'package:huajianshe_app/network/response/BaseResp.dart';
import 'package:huajianshe_app/pages/auth/model/AuthModel.dart';
import 'package:huajianshe_app/pages/main/MomentItem.dart';
import 'package:huajianshe_app/utils/toast_utils.dart';
import 'package:huajianshe_app/widget/SimpleScrollView.dart';
import 'package:huajianshe_app/widget/SmartButton.dart';
import 'package:huajianshe_app/widget/appbar/CAppBar.dart';

class LoginPage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _LoginPageState();
  }
}

class _LoginPageState extends BaseState<LoginPage> {

  TextEditingController usernameController = TextEditingController();
  TextEditingController verificationController = TextEditingController();
  FocusNode usernameNode = FocusNode();
  FocusNode verificationNode = FocusNode();

  @override
  void initState() {
    super.initState();
    // login();
    usernameController.text = '13788951367';
    verificationController.text = '301301';
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // login();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
        appBar: CAppBar(bgColor: Colors.white),
        body: SimpleScrollView(padding: EdgeInsets.symmetric(horizontal: 32), children: [
              H(80),
              Text('登录', style: TS(20, C.gray0D0E15, fontWeight: FontWeight.bold)),
              H(5),
              Padding(padding: EdgeInsets.only(left: 10), child: D(width: 20, height: 3, color: C.primary,)),
              H(19),
              TextField(autofocus: false, controller: usernameController, focusNode: usernameNode,
                style: TS(14, C.gray0D0E15), maxLength: 11, keyboardType: TextInputType.number,
                decoration: InputDecoration(
                    isDense: true,
                    contentPadding: EdgeInsets.only(left: 15, right: 15, top: 15, bottom: 15),
                    fillColor: C.grayF6,
                    filled: true,
                    border: new OutlineInputBorder(
                      borderSide: BorderSide.none,
                      borderRadius: const BorderRadius.all(
                        const Radius.circular(4.0),
                      ),
                    ),
                    labelText: '手机号',
                    labelStyle: TS(12, C.gray666),
                    counterText: ''
                )
              ),
              H(24),
              Stack(children: [
                TextField(autofocus: false, controller: verificationController, focusNode: verificationNode,
                    style: TS(14, C.gray0D0E15), maxLength: 6, keyboardType: TextInputType.number,
                    decoration: InputDecoration(
                        isDense: true,
                        contentPadding: EdgeInsets.only(left: 15, right: 15, top: 15, bottom: 15),
                        fillColor: C.grayF6,
                        filled: true,
                        border: new OutlineInputBorder(
                          borderSide: BorderSide.none,
                          borderRadius: const BorderRadius.all(
                            const Radius.circular(4.0),
                          ),
                        ),
                        labelText: '验证码',
                        labelStyle: TS(12, C.gray666),
                        counterText: ''
                    )
                ),
                Positioned.fill(
                  child: Align(
                      alignment: Alignment.centerRight,
                      child: SmartButton(width: 85, height: 28, fontSize: 13, textColor: C.primary, text: '获取验证码',
                        radius: 14, borderWidth: 1, backgroundColor: Colors.transparent, onPressed: getCode,)
                  ), right: 8,
                ),
              ]),
              H(30),
              SmartButton(text: '登录', fontSize: 16, width: double.infinity, height: 50, radius: 25, onPressed: login,),

              // TextButton(onPressed: () {
              //   AppNavigator.push(context, RouteName.USER_INFO, arguments: {'type': 2,}, onPageResult: (v) {}, clearStack: true);},
              //     child: Text('user info')
              // ),

        ],)
    );
  }

  getCode() {

  }

  login() {
    print('login');
    String username = usernameController.text;
    String code = verificationController.text;

    // username = '13123456789';
    // code = '301301';
    if (username.length < 11 || code.length < 6) {
      Toast.show('请输入正确的手机号和验证码');
      return;
    }
    sendReq(Api.login(), LoginResp(), data: {'username': username, 'verify': code});
  }

  @override
  void onReqSuccess<T>(T t, int code, String msg, String path) {
    if (t is LoginResp) {
      print('sn: ${t.data?.memberSn}');
      App.loginSuccess(t.data?.memberSn ?? '', t.data?.hxId, t.data?.hxPwd);
      AppNavigator.push(context, RouteName.MAIN, clearStack: true, replace: true);
    }
  }
}